﻿namespace uTorrentNAS
{
    using System;
    using System.Collections.Generic;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using Services;
    using Utilities;

    public partial class MainForm : Form
    {
        private readonly ConfigurationService _configurationService;
        private readonly TorrentService _torrentService;

        private bool _serviceStarted = false;

        public MainForm(ConfigurationService configurationService, TorrentService torrentService)
        {
            _configurationService = configurationService;
            _torrentService = torrentService;

            InitializeComponent();
            ShowInTaskbar = false;

            var commandLine = new Arguments(Environment.GetCommandLineArgs());

            var configuration = _configurationService.Retrieve();

            if (configuration == null)
            {
                configuration = new Configuration()
                {
                    UTorrentSettings = new UTorrentSettings()
                    {
                        ServerAddress = null,
                        Username = null,
                        Password = null
                    },
                    ScannerSettings = new ScannerSettings()
                    {
                        StartOnLaunch = false,
                        Interval = 2
                    },
                    Preferences = new Preferences
                    {
                        CleanupFiles = true,
                        CopyFiles = true,
                        ExtractArchives = true,
                        RemoveFinishedTorrent = false,
                        LabelItems = new List<LabelItem>()
                    }
                };

                _configurationService.Save(configuration);
            }

            if (commandLine.Contains("stop") && configuration.ScannerSettings.StartOnLaunch)
            {
                StopService();
            }
            else if (commandLine.Contains("start") || configuration.ScannerSettings.StartOnLaunch)
            {
                StartService();
            }
        }

        private void Exit()
        {
            this.Show();
            this.Close();

            Application.Exit();
        }

        private void menuItemExit_Click(object sender, EventArgs e)
        {
            Exit();
        }

        private void menuItemSettings_Click(object sender, EventArgs e)
        {
            OpenSettings();
        }

        private void menuItemToggleServiceStatus_Click(object sender, EventArgs e)
        {
            if (_serviceStarted) StopService();
            else StartService();
        }

        private void StartService()
        {
            try
            {
                Task.Run(() => _torrentService.Start());

            }
            catch (Exception ex)
            {
                WinAPI.MessageBox("Could not start service: " + ex.Message, "Error");
                return;
            }
            _serviceStarted = true;

            menuItemToggleServiceStatus.Text = "&Stop Service";
        }

        private void StopService()
        {
            try
            {
                _torrentService.Stop();
            }
            catch (Exception ex)
            {
                WinAPI.MessageBox("Could not stop service: " + ex.Message, "Error");
                return;
            }
            _serviceStarted = false;


            menuItemToggleServiceStatus.Text = "&Start Service";
        }

        protected override void SetVisibleCore(bool value)
        {
            base.SetVisibleCore(false);
        }

        private void notificationIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            OpenSettings();
        }

        private void OpenSettings()
        {
            var settingsForm = new SettingsForm(_configurationService, _torrentService);
            settingsForm.Show();
        }

    }
}
